﻿namespace FilteredPageReference
{
    using System;
    using System.Web;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using EPiServer;
    using EPiServer.Core.PropertySettings;
    using EPiServer.SpecializedProperties;
    using EPiServer.Web.PropertyControls;
    using UserControls;

    /// <summary>
    /// Property filtered link collectioncontrol
    /// </summary>
    [PropertySettings(typeof(PropertyFilteredPageReferenceSettings), true)]
    public class PropertyFilteredLinkCollectionControl : PropertyDataControl
    {

        private PropertyLinkCollectionFilteredPageReferenceEditControl _editControl;

        /// <summary>
        /// Applies the edit changes.
        /// </summary>
        public override void ApplyEditChanges()
        {
            try
            {
                PropertyLinkCollection.Links = ((IPropertyLinkCollectionEditControl)_editControl).NewLinkCollection;
            }
            catch (Exception exception)
            {
                AddErrorValidator(exception.Message);
            }
        }

        /// <summary>
        /// Creates the default controls.
        /// </summary>
        public override void CreateDefaultControls()
        {
            CopyWebAttributes(this);
            HtmlGenericControl child = new HtmlGenericControl("ul");

            if (!string.IsNullOrEmpty(CssClass))
                child.Attributes.Add("class", CssClass);

            Controls.Add(child);

            foreach (LinkItem item in PropertyLinkCollection)
            {
                HtmlGenericControl listItem = new HtmlGenericControl("li");
                Literal literal = new Literal { Text = item.ToMappedLink() };
                listItem.Controls.Add(literal);
                child.Controls.Add(listItem);
            }
        }

        /// <summary>
        /// Creates the edit controls.
        /// </summary>
        public override void CreateEditControls()
        {
            PropertyFilteredPageReferenceSettings settings = GetSettings();
            string sessionKey = string.Empty;

            if (PropertyData.PageDefinitionID == 0)
            {
                sessionKey = Guid.NewGuid().ToString("N") + "_" + PropertyData.Name;
                HttpContext.Current.Session[sessionKey] = settings;
            }

            _editControl = Page.LoadControl("~/util/FilteredPageReference/PropertyLinkCollectionFilteredPageReferenceEditControl.ascx") as PropertyLinkCollectionFilteredPageReferenceEditControl;
            _editControl.SessionKey = sessionKey;
            _editControl.FPRData = string.Format("{0}|{1}|{2}|{3}|{4}|{5}",
                                                 CurrentPage.PageLink.ID,
                                                 CurrentPage.PageLink.WorkID,
                                                 CurrentPage.LanguageBranch, 
                                                 CurrentPage.PageTypeID,
                                                 CurrentPage.ParentLink.ID,
                                                 PropertyData.Name);

            _editControl.SetLinkEditorUrl();
            ((IPropertyLinkCollectionEditControl)_editControl).OriginalLinkCollection = PropertyLinkCollection.Links;
            Controls.Add(_editControl);
            ((IPropertyLinkCollectionEditControl)_editControl).OriginalLinkCollection = PropertyLinkCollection.Links;
        }

        /// <summary>
        /// Gets the property link collection.
        /// </summary>
        private PropertyLinkCollection PropertyLinkCollection
        {
            get
            {
                return (PropertyData as PropertyLinkCollection);
            }
        }

        /// <summary>
        /// Gets the properrty settings.
        /// </summary>
        /// <returns></returns>
        private PropertyFilteredPageReferenceSettings GetSettings()
        {
            PropertyFilteredPageReferenceSettings settings = null;

            if (PropertyData.SettingsContainer != null)
            {
                PropertySettingsWrapper settingsWrapper = PropertyData.SettingsContainer.GetSetting(typeof(PropertyFilteredPageReferenceSettings));

                if (settingsWrapper != null)
                    settings = settingsWrapper.PropertySettings as PropertyFilteredPageReferenceSettings;
            }

            if (settings == null)
                settings = new PropertyFilteredPageReferenceSettings();

            return settings;
        }

    }
}